<%@ tag language="java" pageEncoding="UTF-8" body-content="empty"%>
<%@ attribute name="name" type="java.lang.String" required='true' description="表单name"%>
<%@ attribute name="id" type="java.lang.String" required='true' description="上传组件id"%>
<%@ attribute name="url" type="java.lang.String" required='true' description="上传文件访问后台的链接"%>
<%@ attribute name="swf" type="java.lang.String" required='false' description="自定义使用的flash文件"%>
<%@ attribute name="debug" type="java.lang.Boolean" required='false' description="是否使用debug模式"%>
<%@ attribute name="buttonText" type="java.lang.String" required='false' description="上传按钮上显示的文字"%>
<%@ attribute name="multiple" type="java.lang.Boolean" required='false' description="是否允许上传多个文件，默认不允许"%>
<%@ attribute name="autoUpload" type="java.lang.Boolean" required='false' description="是否允许自动上传，默认不允许"%>
<%@ attribute name="fileSizeLimit" type="java.lang.Long" required='false' description="设置允许上传的单个文件最大的文件大小，default=20971520，即20M"%>
<%@ attribute name="uploadLimit" type="java.lang.Integer" required='false' description="设置最多允许同时上传的文件个数，仅当设置multiple=true时有效，默认为1"%>
<%@ attribute name="removeTimeout" type="java.lang.Integer" required='false' description="设置上传完成后从上传队列中移除的时间（单位：秒）"%>
<%@ attribute name="successTimeout" type="java.lang.Integer" required='false' description="表示uploadify的成功等待时间（单位：秒，默认30秒）"%>
<%@ attribute name="buttonWidth" type="java.lang.Integer" required='false' description="上传按钮的宽度"%>
<%@ attribute name="buttonHeight" type="java.lang.Integer" required='false' description="上传按钮的高度"%>
<%@ attribute name="buttonClass" type="java.lang.String" required='false' description="自定义上传按钮样式，如'btn btn-primary'"%>
<%@ attribute name="fileObjName" type="java.lang.String" required='false' description="自定义上传文件表单名，默认‘Filedata’"%>
<%@ attribute name="icon" type="java.lang.String" required='false' description="自定义上传图标，默认为'icon-upload-alt',设置为'none'则取消图标"%>
<%@ attribute name="fileTypeDesc" type="java.lang.String" required='false' description="可选择文件类型描述，此字符串出现在浏览文件对话框的文件类型下拉菜单中。如'Image Files'"%>
<%@ attribute name="fileTypeExts" type="java.lang.String" required='false' description="定义允许上传的文件后缀。如'*.gif; *.jpg; *.png'"%>
<%@ attribute name="removeCompleted" type="java.lang.String" required='false' description="设置为false,则上传列表不消失，默认true"%>
<%@ attribute name="onFallback" type="java.lang.String" required='false' description="检测上传环境不允许或不支持时，此回调函数会触发"%>
<%@ attribute name="onUploadSuccess" type="java.lang.String" required='false' description="绑定当单个上传成功时的回调，onUploadSuccess(file, data, response)"%>
<%@ attribute name="onSelect" type="java.lang.String" required='false' description="绑定当选择一个文件时触发的事件，onSelect(file)"%>
<%@ attribute name="onError" type="java.lang.String" required='false' description="绑定当上传失败时的回调，onError(event, queueID, fileObj)"%>
<%@ attribute name="onQueueComplete" type="java.lang.String" required='false' description="绑定当所有上传完成时的回调，onQueueComplete(queueData)"%>
<%@ attribute name="onDialogOpen" type="java.lang.String" required='false' description="在浏览文件对话框被打开前一瞬触发该事件"%>
<%@ attribute name="onDialogClose" type="java.lang.String" required='false' description="在浏览文件对话框关闭时触发该事件,onDialogCloseFun(queueData,fileTypeExts)"%>
<%@ attribute name="onUploadStart" type="java.lang.String" required='false' description="开始上传前触发该事件,onUploadStartFun(file)"%>
<input type="file" name="${name}" id="${id}"/>
<input id="uploadify_sessionId" type="hidden" value="${pageContext.session.id}"/> 
<div id="fileQueue"></div>	
<script type="text/javascript">
(function(){
	var sid = $('#uploadify_sessionId').val();
	var url = "${url}"+";jsessionid="+sid;
	var swf = "${swf}"==""?ctxStatic+'/lib/uploadify/uploadify.swf':"${swf}";
	var debug = "${debug}"=="true"?true:false;
	var buttonText = "${buttonText}"==""?"选择文件":"${buttonText}";
	var multiple = "${multiple}"=="true"?true:false;
	var autoUpload = "${autoUpload}"=="true"?true:false;
	var fileObjName = "${fileObjName}"==""?"Filedata":"${fileObjName}";
	var fileSizeLimit = "${fileSizeLimit}"==""?(20*1024*1024):parseInt("${fileSizeLimit}");
	var uploadLimit = "${uploadLimit}"==""?1:parseInt("${uploadLimit}");
	var onFallback = "${onFallback}"==""?undefined:eval("${onFallback}");
	var onUploadSuccess = "${onUploadSuccess}"==""?undefined:eval("${onUploadSuccess}");
	var onSelect = "${onSelect}"==""?undefined:eval("${onSelect}");
	var onError = "${onError}"==""?undefined:eval("${onError}");
	var removeTimeout = "${removeTimeout}"==""?0:parseInt("${removeTimeout}");
	var successTimeout = "${successTimeout}"==""?30:parseInt("${successTimeout}");
	var buttonWidth = "${buttonWidth}"==""?undefined:parseInt("${buttonWidth}");
	var buttonHeight = "${buttonHeight}"==""?undefined:parseInt("${buttonHeight}");
	var onQueueComplete = "${onQueueComplete}"==""?undefined:eval("${onQueueComplete}");
	var buttonClass = "${buttonClass}";
	var uploadLimitS = uploadLimit;
	var fileTypeDesc = "${fileTypeDesc}"==""?undefined:"${fileTypeDesc}";
	var fileTypeExts = "${fileTypeExts}"==""?undefined:"${fileTypeExts}";
	var removeCompleted = "${removeCompleted}"==""?true:"${removeCompleted}"=="true";
	var icon = "${icon}"==""?"icon-upload-alt":"${icon}";
	var onDialogOpenFun = "${onDialogOpen}"==""?undefined:eval("${onDialogOpen}");
	var onDialogCloseFun = "${onDialogClose}"==""?undefined:eval("${onDialogClose}");
	var onUploadStartFun = "${onUploadStart}"==""?undefined:eval("${onUploadStart}");
	if(icon == 'none'){
		icon == "";
	}
	buttonTextS = "<i class='"+icon+"'>"+buttonText+"</i>";
	//加载上传插件
	$("#${id}").uploadify({
		'buttonClass' : buttonClass,
	   'swf'       : swf,
		'debug'		: debug,
	   'uploader'  :  url,
	   'buttonText': buttonTextS,
	   'multi'		: multiple,
	   'auto'		: autoUpload,
	   'fileSizeLimit':fileSizeLimit,
	   'uploadLimit'  : uploadLimitS, 
	   'removeTimeout':removeTimeout,
	   'successTimeout':successTimeout,
	   'width':buttonWidth,
	   'height':buttonHeight,
	   'fileTypeDesc':fileTypeDesc,
	   'fileTypeExts':fileTypeExts,
	   'removeCompleted':removeCompleted,
	   'fileObjName':fileObjName,
	   'onFallback':function(){
	       MsgBox.error("您未安装FLASH控件，无法上传图片！请安装FLASH控件后再试。");
	       onFallback && onFallback();
	   },
	   'onSelectError': function (file, errorCode, errorMsg) {
	       switch (errorCode) {
	           case -100:
	        	   MsgBox.error("上传的文件数量已经超出系统限制的"+uploadLimit+"个文件！");
	               break;
	           case -110:
	               MsgBox.error("文件 [" + file.name + "] 大小超出系统限制的" + $('#file_upload').uploadify('settings', 'fileSizeLimit') + "大小！");
	               break;
	           case -130:
	        	   MsgBox.error("文件 ["+file.name+"] 格式不正确，只允许上传 "+fileTypeExts+" 格式文件");
	        	   break;
	       }
	       return false;
	   },
	   'onUploadSuccess':function(file, data, response){
	   	//上传成功后，将后台生成的随机文件名赋值到对应属性中
	   		//if(true == response){
	   			//重新设置下载队列
	   			$("#${id}").uploadify('settings','uploadLimit',++uploadLimitS);
	   		//}
	   		onUploadSuccess&& onUploadSuccess(file, data, response);
		    /* var randomName = data;
		    $("#attachAddress").val(randomName);
		    $("#attachType").val(file.type);
		    $("#ff").submit(); */
	    },
	    'onSelect':function(file){
	    	onSelect && onSelect(file);
	      	//选中文件后，将文件名称显示到标题里或赋值
	    	/* var fileName = file.name.indexOf(".")>0?file.name.substring(0,file.name.lastIndexOf(".")):file.name;
	    	$("#attachName").val(fileName); */
	    },
	    'onError': function(event, queueID, fileObj) {
	         MsgBox.error("文件:" + fileObj.name + "上传失败");
	         onError && onError(event, queueID, fileObj);
	    },
	    'onQueueComplete' : function(queueData) {
	    	onQueueComplete && onQueueComplete(queueData);
	    },
	    'onDialogOpen' : function(){
	    	onDialogOpenFun && onDialogOpenFun();
	    },
	    'onDialogClose' : function(queueData){
	    	onDialogCloseFun && onDialogCloseFun(queueData,fileTypeExts);
	    },
	    'onUploadStart':function(file){
	    	onUploadStartFun && onUploadStartFun(file);
	    }
	});
	window["${id}"] = $("#${id}").data('uploadify');
})();
</script>


